﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CreateUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Session["isValid"].ToString().Equals("true"))
                Response.Redirect("login.aspx");
        }
        catch (Exception ex)
        {
            Exception ex2 = ex;
            Response.Redirect("login.aspx");
        }

        DataClassesDataContext dc = new DataClassesDataContext();
        User usr =
            (from u in dc.Users
             where u.Username.Equals(Session["username"])
             select u).Single();
        if (!usr.Usertype.Equals("adm"))
            Response.Redirect("default.aspx");
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!this.TextBox2.Text.Equals(this.TextBox3.Text))
        {
            HttpContext.Current.Session["message"] = "Τα συνθηματικά δεν ταιριάζουν.";
            return;
        }

        if( (this.TextBox1.Text.Length == 0 ) )
        {
            HttpContext.Current.Session["message"] = "Εισάγετε όνομα χρήστη.";
            return;
        }

        if ((this.TextBox2.Text.Length == 0))
        {
            HttpContext.Current.Session["message"] = "Εισάγετε συνθηματικό";
            return;
        }

        if ((this.TextBox4.Text.Length == 0))
        {
            HttpContext.Current.Session["message"] = "Εισάγετε διεύθυνση email.";
            return;
        }


        DataClassesDataContext dc = new DataClassesDataContext();
        decimal? rvalue = 0;
        try
        {
            rvalue = dc.userRegister(this.TextBox1.Text.Trim(), this.TextBox2.Text.Trim(), this.TextBox4.Text.Trim(), this.TextBox5.Text.Trim(), this.TextBox6.Text.Trim());
        }
        catch (Exception ex)
        {
            Exception ex2 = ex;
            HttpContext.Current.Session["message"] = "Το όνομα χρήστη ή η διεύθυνση email υπάρχουν ήδη.";
            return;
        }
        if (rvalue == 0)
        {
            HttpContext.Current.Session["message"] = "Ο χρήστης δημιουργήθηκε.";
            Response.Redirect("default.aspx");
        }
        else
            HttpContext.Current.Session["message"] = "Η δημιουργία χρήστη απέτυχε.";
    }
}